package com.atguigu.eduservice.controller;


import com.atguigu.commonutils.Result;
import com.atguigu.eduservice.entity.EduCourse;
import com.atguigu.eduservice.entity.EduTeacher;
import com.atguigu.eduservice.entity.vo.CourseInfoVo;
import com.atguigu.eduservice.entity.vo.CoursePublishVo;
import com.atguigu.eduservice.entity.vo.TeacherQuery;
import com.atguigu.eduservice.service.EduCourseService;
import com.atguigu.eduservice.service.EduTeacherService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;

/**
 * <p>
 * 课程 前端控制器
 * </p>
 *
 * @author 董万均
 * @since 2020-10-25
 */
@RestController
@RequestMapping("/eduservice/course")
public class EduCourseController {
    @Autowired
    private EduCourseService courseService;

    //添加课程信息
    @PostMapping("addCourseInfo")
    public Result addCourseInfo(@RequestBody CourseInfoVo courseInfoVo){//返回的数据是请求体
        String id=courseService.saveCourseInfo(courseInfoVo);
        return Result.ok().data("courseId",id);
    }

    //根据courseId获取id的信息
    @GetMapping("getCourseInfo/{courseId}")
    public Result getCourseInfo(@PathVariable String courseId){
        CourseInfoVo courseInfoVo=courseService.getCourseInfoById(courseId);
        return Result.ok().data("courseInfo",courseInfoVo);
    }

    //修改课程信息
    @PostMapping("updateCourseInfo")
    public Result updateCourseInfo(@RequestBody CourseInfoVo courseInfoVo){
        courseService.updateCourseInfo(courseInfoVo);
        return Result.ok();
    }

    //查询所有的课程信息
    @GetMapping("getPublishInfo/{courseId}")
    public Result getPublishInfo(@PathVariable String courseId){
        CoursePublishVo coursePublishVo=courseService.getPublishInfo(courseId);
        return Result.ok().data("publishInfo",coursePublishVo);
    }

    //课程的最终发布
    @PostMapping("publishCourse/{courseId}")
    public Result publishCourse(@PathVariable String courseId){
        EduCourse eduCourse=new EduCourse();
        eduCourse.setId(courseId);
        eduCourse.setStatus("Normal");//发布的状态是Normal
        courseService.updateById(eduCourse);
        return Result.ok();
    }

    //课程的条件查询
    @PostMapping("pageCourseCondition/{current}/{limit}")
    public Result pageTeacherCondition(@PathVariable long current, @PathVariable long limit,
                                       @RequestBody(required = false) EduCourse eduCourse){
        Page<EduCourse> coursePage=new Page<>(current,limit);
        QueryWrapper<EduCourse> wrapper=new QueryWrapper<>();
        String title = eduCourse.getTitle();//课程名称
        String status = eduCourse.getStatus();//课程状态
        //动态拼接SQL语句
        if (!StringUtils.isEmpty(title)){
            wrapper.like("title",title);
        }
        if (!StringUtils.isEmpty(status)){
            if (status.equals("1")){
                status="Draft";
            }else {
                status="Normal";
            }
            wrapper.eq("status",status);
        }
        //排序，将最新创建的排在第一个
        wrapper.orderByDesc("gmt_create");
        courseService.page(coursePage,wrapper);
        long total=coursePage.getTotal();
        List<EduCourse> records = coursePage.getRecords();
        return Result.ok().data("total",total).data("list",records);
    }

    //删除课程，需要依次删除小结表、章节表、课程描述表、课程表
    @DeleteMapping("deleteCourse/{courseId}")
    public Result deleteCourse(@PathVariable String courseId){
        courseService.removeCourseById(courseId);
        return Result.ok();
    }

}

